﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace csharp_http_post
{
    class Program
    {
        static void Main(string[] args)
        {
            string strUri, userid, password;

            Console.Write("input url: ");
            strUri = Console.ReadLine();

            byte[] postData = Encoding.ASCII.GetBytes("param1=value1&param2=value2");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUri);
            request.Method = WebRequestMethods.Http.Post;
            request.ContentLength = postData.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            using(Stream stream = request.GetRequestStream()){
                stream.Write(postData, 0, postData.Length);
            }

            HttpWebResponse response;

            try
            {
                response = (HttpWebResponse)request.GetResponse();

                WebHeaderCollection headerCollection = response.Headers;

                Stream stream = response.GetResponseStream();
                StreamReader streamReder = new StreamReader(stream, Encoding.Default);

                string strBody = streamReder.ReadToEnd();

                Console.WriteLine(">>>>>>>>>>>>>>> header");
                foreach (string headerKey in headerCollection.AllKeys)
                {
                    Console.Write(headerKey);
                    Console.Write(": ");
                    Console.WriteLine(headerCollection[headerKey]);
                }

                Console.WriteLine(">>>>>>>>>>>>>>> body");
                Console.WriteLine(strBody);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            
            Console.Write("Press Any Key");
            Console.ReadKey();
        }
    }
}
